package com.wj.ballcat.demo.controller;

import com.hccake.ballcat.common.log.operation.annotation.CreateOperationLogging;
import com.hccake.ballcat.common.model.result.BaseResultCode;
import com.hccake.ballcat.common.model.result.R;
import com.wj.ballcat.demo.model.entity.Article;
import com.wj.ballcat.demo.reposity.ArticleRepository;
import com.wj.ballcat.demo.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequiredArgsConstructor
@RequestMapping("/demo/article")
@Tag(name = "文章")
public class ArticleController {

	private final ArticleService articleService;
	private final ArticleRepository repository;

	/**
	 * 新增
	 * @param article
	 * @return R 通用返回体
	 */
	@Operation(summary = "新增")
	@CreateOperationLogging(msg = "新增")
	@PostMapping
//	@PreAuthorize("@per.hasPermission('demo:demo:add')")
	public R<Void> save(@RequestBody Article article) {
		return articleService.save(article) ? R.ok() : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "新增失败");
	}

	@GetMapping()
	public void add (String title, String content){
		repository.save(new Article().setTitle(title).setContent(content));
	}
}
